# coding=utf-8
class Step(object):
    def __init__(self, name, py_script, input_parameters):
        self.name = name
        self.py_script = py_script
        self.input_parameters = input_parameters
        # self.data_path = data_path  # data_path support list
        # self.output_path = output_path
        # self.enable_timestamp = enable_timestamp
        self.deploy_mode = "cluster"  # client/client
        self.action_on_failure = "CONTINUE"  # TERMINATE_CLUSTER/CONTINUE
        self.packages = [
            "org.mongodb.spark:mongo-spark-connector_2.11:2.4.1",
            "org.postgresql:postgresql:9.4.1211"
        ]

    def f(self):
        # output_path_list =
        #args = {"Args":["spark-submit", "--deploy-mode","cluster","--py-files","s3://pt-emr-common//SourceCode/common_emr.zip",self.py_script,"--data_path",self.data_path,"--output_path",self.output_path],"Type":"CUSTOM_JAR","ActionOnFailure":"TERMINATE_CLUSTER","Jar":"command-runner.jar","Properties":"","Name":"Spark application"}
        #args = {"Args":["spark-submit", "--deploy-mode","cluster","--py-files","s3://pt-emr-common/SourceCode/common_emr.zip","--packages","net.snowflake:snowflake-jdbc:3.0.14,net.snowflake:spark-snowflake_2.11:2.1.3",self.py_script,"--data_path","--output_path",self.output_path],"Type":"CUSTOM_JAR","ActionOnFailure":"TERMINATE_CLUSTER","Jar":"command-runner.jar","Properties":"","Name":"Spark application"}
        output = {
                "Name": "Spark_application:%s" % self.name,
                "ActionOnFailure": self.action_on_failure,
                'HadoopJarStep': {
                    "Properties": [],
                    "Jar": "command-runner.jar",
                    "Args": ["spark-submit", "--deploy-mode", self.deploy_mode, "--py-files",
                             "s3://pt-emr-common/SourceCode/release/common.zip", "--packages", ",".join(self.packages),
                             self.py_script,
                             "--parameters"],
                    },
                }

        input_parameters_index = output["HadoopJarStep"]["Args"].index("--parameters")
        if isinstance(self.input_parameters, list):
            self.input_parameters.reverse()
            for param in self.input_parameters:
                output["HadoopJarStep"]["Args"].insert(input_parameters_index + 1, param)
        elif isinstance(self.input_parameters, str):
            output["HadoopJarStep"]["Args"].insert(input_parameters_index + 1, self.input_parameters)
        else:
            raise Exception("type must be list or str")
        return output
